Repository navigation
Fix string handling in pushAny - #231
Conversation
robbielyman
left a comment
There was a problem hiding this comment.
Some of these changes are sort of surprising to me!
| } | ||
| _ = lua.pushStringZ(std.mem.span(value)); | ||
| }, | ||
| .c => @compileError("C pointer length cannot be determined; pass a slice instead"), |
There was a problem hiding this comment.
why is this here? Lua itself obviously works with C pointers all the time?
There was a problem hiding this comment.
Quoting the Zig documentation: "The only valid reason for using a C pointer is in auto-generated code from translating C code."
I see no reason why they should be supported. The caller should know the type.
There was a problem hiding this comment.
It is possible that the value comes from another C library. I have recently been doing work using Lua and sqlite and I need to work with C strings occasionally. I think C pointers should be supported
There was a problem hiding this comment.
In that case, you would either know the length or whether it has a null terminator, then you could use either ptr[0..len] or std.mem.span(ptr).
If we supported C pointers with pushAny, it would be making the assumption that it has a null terminator, which results in UB when there isn't one.
Side note: I did not remove support for C pointers in this change. I only changed the error message.
There was a problem hiding this comment.
I just tested with this PR. This fails
const c_str: [*c]const u8 = "hello zig";
try lua.pushAny(c_str);
try testing.expectEqualStrings("hello zig", try lua.toString(-1));and both of these work
const c_str: [*c]const u8 = "hello zig";
try lua.pushAny(std.mem.span(c_str));
try testing.expectEqualStrings("hello zig", try lua.toString(-1));
const c_str: [*c]const u8 = "hello zig";
const ptr: [*:0]const u8 = c_str;
try lua.pushAny(ptr);
try testing.expectEqualStrings("hello zig", try lua.toString(-1));I gave it more thought and I think I'm actually okay with this change. If someone has a [*c] pointer, they can use span or coerce to a null-terminated many-item pointer.
|
@natecraddock Have you had a chance to look at my reply? Let me know if there are any changes you'd like me to make. |
|
@AshAmetrine yes I took a look. I gave it more thought and I am okay with these changes. Tests are failing because this branch is outdated so I'll rebase on main and get it passing and merged. |
58e3dc9 to
3313b4d
Compare
|
Merged! Sorry for taking a while to take another look |
Changes:
[*:0]const u8)pushAnytest to cover support for[*:0]const u8and embedded null bytes in string literals